﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace ShopOnline.Controls
{
    public partial class ProductDetails : BaseControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public event CartChangedEventHandler CartChanged;
        private void OnCartChanged(EventArgs e)
        {
            if (CartChanged != null)
                CartChanged(this, e);
        }

        POSSystem.BLL.View_WebProducts product;

        public POSSystem.BLL.View_WebProducts Product
        {
            get { return product; }
            set
            {
                product = value;
                frmProduct.DataSource = product.DefaultView;
                frmProduct.DataBind();
                LightBoxGallery gallery = (LightBoxGallery)frmProduct.FindControl("gallery");

                ProductImages imgs = new ProductImages();
                imgs.Search(ProductImages.ColumnNames.ProductID + "=" + product.ID + " and Deleted = 0", "*", "", 0, 99);

                gallery.DataSource = imgs.DefaultView;
                gallery.DataBind();
            }
        }

        public string GetRPPPrice()
        {
            return string.IsNullOrEmpty(product.s_RegularPrice) ? "" : "<span class='productDetailLabel' style=' font-weight:bold; text-decoration:line-through'>Regular Price: <span class='productDetailLabel' style='color:red; font-weight:bold; text-decoration:line-through'>" + string.Format("{0:C}", product.RegularPrice) + "</span></span><br/>";
        }

        public string GetPrice()
        {
            if (View_Customers.GetCurrentUser() == null)
                return string.Format("{0:C}", product.SalePrice);

            return string.Format("{0:C}", (View_Customers.GetCurrentUser().Price == CustomerType.CustomerPrice.SalePrice) ? product.SalePrice : (View_Customers.GetCurrentUser().Price == CustomerType.CustomerPrice.DealerPrice ? product.DealerPrice : product.PartnerPrice));
        }

        public string GetRawPrice()
        {
            if (View_Customers.GetCurrentUser() == null)
                return string.Format("{0:C}", product.SalePrice);
            return string.Format("{0}", (View_Customers.GetCurrentUser().Price == CustomerType.CustomerPrice.SalePrice) ? product.SalePrice : (View_Customers.GetCurrentUser().Price == CustomerType.CustomerPrice.DealerPrice ? product.DealerPrice : product.PartnerPrice));
        }

        public string GetTags(int productID)
        {
            ProductTags tag = ProductTags.GetTags(productID);
            string html = "";
            for (int i = 0; i < tag.RowCount; i++)
            {
                html += string.Format("<a class='tag'>{0}</a>", tag.s_Tag);
                tag.MoveNext();
            }

            return html == "" ? "n/a" : html;
        }

        public string GetProductName()
        {
            return product.s_ProductName;
        }

        public string GetStatus()
        {
            return product.Quantity > 0 ? "<span style='color:#ff339c'>In Stock </span><img align='top' src='" + this.ResolveClientUrl("~/images/icon_check.gif") + "' />" : "<span style='color:grey'>Back Order</span>";
        }

        public string GetPicture()
        {
            ProductImages img = ProductImages.GetDefaultPicture(product.ID);
            return img.RowCount == 0 || img.s_OriginalUrl == "" ? "~/images/photo_not_available.jpg" : ShopOnline.Utilities.GetImageUrl(img.s_OriginalUrl.ToString());
        }

        public string GetPictureID()
        {
            ProductImages img = ProductImages.GetDefaultPicture(product.ID);
            return img.RowCount == 0 ? "0" : img.s_ID;
        }

        protected void btnAddWishList_OnClick(object sender, EventArgs e)
        {
            if (View_Customers.GetCurrentUser() != null && View_Customers.GetCurrentUser().RowCount > 0)
            {
                int id = int.Parse(hidID.Value);
                POSSystem.BLL.WishList w = POSSystem.BLL.WishList.GetByCustomer(View_Customers.GetCurrentUser().ID, id);
                if (w.RowCount > 0)
                {
                    ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showNotification('All For You Online', 'This item has already been added to your WishList!', 8000);", 1), true);
                    return;
                }
                w.AddNew();
                w.CustomerID = View_Customers.GetCurrentUser().ID;
                w.ProductID = id;
                w.CreatedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
                w.Deleted = false;
                w.Save();

                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showNotification('All For You Online', 'Your WishList has been updated!', 8000);", 1), true);
            }
            else
                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showErrorAlert('All For You Online', 'You have to login first!', 8000);", 1), true);
        }

        protected void btnAddCart_OnClick(object sender, EventArgs e)
        {
            int id = int.Parse(hidID.Value);

            View_WebProducts p = View_WebProducts.LoadProductByID(id);

            OrderItems items = BasePage.SelectedItems;
            items.AddNew();
            if (!items.DefaultView.Table.Columns.Contains(BasePage.PRODUCT_IMAGE))
                items.AddColumn(BasePage.PRODUCT_IMAGE, typeof(string));
            items.ProductID = id;
            items.ID = -items.RowCount;
            items.ProductName = p.s_ProductName;
            items.Quantity = 1;
            items.s_CustomerPrice = (SessionUser != null && SessionUser.Price == CustomerType.CustomerPrice.DealerPrice) ? p.s_DealerPrice : ((SessionUser != null && SessionUser.Price == CustomerType.CustomerPrice.PartnerPrice) ? p.s_PartnerPrice : p.s_SalePrice);
            items.s_UnitCost = p.s_Cost;
            items.s_UnitPrice = p.s_SalePrice;
            items.s_UnitDealerPrice = p.s_DealerPrice;
            items.TotalAmount = items.Quantity * items.CustomerPrice;
            items.GST = p.GST ? Math.Round(items.TotalAmount * SystemVariables.SysVars.GST / (SystemVariables.SysVars.GST + 100), 2) : 0;
            items.Deleted = false;
            items.BarCode = p.s_BarCode;
            items.s_ProductImage = p.s_ProductImageID;
            items.Category = OrderItemCategory.Product;
            items.Name = p.ProductName;
            if (items.s_ProductImage != "")
            {
                ProductImages img = ProductImages.GetById(int.Parse(items.s_ProductImage));
                if (img.RowCount > 0)
                    items.SetColumn(BasePage.PRODUCT_IMAGE, ShopOnline.Utilities.GetImageUrl(img.s_ThumbUrl));
            }
            else
                items.SetColumn(BasePage.PRODUCT_IMAGE, GetDefaultPic());

            OnCartChanged(e);
            ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showNotification('All For You Online', 'Your cart has been updated!', 8000);", 1), true);
        }

    }
}